package com.icss.ah.storeApply.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.icss.ah.base.action.BaseAction;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.storeApply.service.StoreApplyService;
import com.icss.j2ee.util.UUID;

/**
 * 店铺申请控制类
 * 1、基础信息保存
 * 2、经营信息保存
 * 3、开户信息保存
 * @date 2017-8-10
 */
@Controller
@Scope("prototype")
public class StoreApplyAction extends BaseAction {
	
	private static final long serialVersionUID = -5339900958843950245L;
	
	@Autowired
	private StoreApplyService storeApplyService;
	
	private String shopId;
	private String status;

	/**
	 * 基础信息页面
	 */
	public String toStoreApplyOnePage(){
		request = this.getRequest();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		shopId = shopId == null || "".equals(shopId) ? userInfo.getShopId() : shopId;
		Map<String, Object> shopInfo = new HashMap<String, Object>();
		Map<String, String> map = new HashMap<String, String>();
		map.put("level", "2");
		List<Map<String, Object>> provinceList = storeApplyService.queryProvinceCityList(map);
		request.setAttribute("provinceList", provinceList);
		if(!"".equals(shopId)){
			map.put("shopId", shopId);
			shopInfo = storeApplyService.queryShopInfo(map);
			map.put("areaPcode",(String) shopInfo.get("SHOP_PROVINCE"));
			map.put("level", "3");
			List<Map<String, Object>> cityList = storeApplyService.queryProvinceCityList(map);
			map.put("areaPcode",(String) shopInfo.get("SHOP_CITY"));
			map.put("level", "4");
			List<Map<String, Object>> countyList = storeApplyService.queryProvinceCityList(map);
			request.setAttribute("cityList", cityList);
			request.setAttribute("countyList", countyList);
		}
		request.setAttribute("shopInfo", shopInfo);
		return "toStoreApplyOnePage";
	}
	
	private String level;		//级次
	private String areaPcode;	//父编码
	
	/**
	 * 省市县区查询
	 * @date 2017-8-15
	 */
	public void queryProvinceCityList(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("level", level);
		map.put("areaPcode", areaPcode);
		List<Map<String, Object>> list = storeApplyService.queryProvinceCityList(map);
		str2Resp(JSONArray.toJSONString(list));
	}
	
	private String shopName;
	private String legalPerson;
	private String shopProvince;
	private String shopCity;
	private String shopCounty;
	private String shopAddr;
	private String shopTel;
	private String shopDesc;
	private String idcardF;
	private String idcardB;
	
	/**
	 * 基础信息保存
	 * @date 2017-8-15
	 */
	public void saveOne(){
		String userCode=getUserCodeFromSession();
		Map<String, String> map = getPara();
		map.put("userCode", userCode);
		map.put("shopId", shopId);
		map.put("shopName", shopName);
		map.put("legalPerson", legalPerson);
		map.put("shopProvince", shopProvince);
		map.put("shopCity", shopCity);
		map.put("shopCounty", shopCounty);
		map.put("shopAddr", shopAddr);
		map.put("shopTel", shopTel);
		map.put("shopDesc", shopDesc);
		map.put("shopLogo", shopLogo);
		map.put("shopPic", shopPic);
		map.put("idcardF", idcardF);
		map.put("idcardB", idcardB);
		map.put("status", status);
		shopStatus = "3".equals(shopStatus) ? "3" : "1";
		map.put("shopStatus", shopStatus);
		String result = storeApplyService.saveOne(map);
		String[] str = result.split(",");
		JSONObject json = new JSONObject();
		if(str[0].equals("1")){
			json.put("result", "保存成功");
			json.put("shopId", str[1]);
		} else {
			json.put("result", "保存失败");
		}
		str2Resp(json.toString());
	}
	
	/**
	 * 店铺申请经营信息页
	 * @date 2017-8-15
	 */
	public String toStoreApplyTwoPage(){
		request = this.getRequest();
		String areaName = "";
		String areaCode = "";
		Map<String, String> map = new HashMap<String, String>();
		map.put("shopId", shopId);
		Map<String, Object> shopInfo = storeApplyService.queryShopInfo(map);
		List<Map<String, Object>> deliveryFeeList = storeApplyService.queryDeliveryFee(map);
		List<Map<String, Object>> areaCodes = storeApplyService.queryCheckList(map);
		if(areaCodes.size() > 0){
			for (Map<String, Object> aMap : areaCodes) {
				areaCode += aMap.get("AREA_CODE").toString() + ",";
				areaName += aMap.get("ANAME").toString() + ",";
			}
			if(!"".equals(areaCode)){
				areaCode = areaCode.substring(0,areaCode.length() - 1);
				areaName = areaName.substring(0,areaName.length() - 1);
			}
		}
		request.setAttribute("shopInfo", shopInfo);
		request.setAttribute("deliveryFeeList", deliveryFeeList);
		request.setAttribute("areaCode", areaCode);
		request.setAttribute("areaName", areaName);
		return "toStoreApplyTwoPage";
	}
	
	private String shopArea;
	private String shopLogo;
	private String shopPic;
	private String businessLicense;
	private String businessScope;
	private String pointX;					//经度
	private String pointY;					//维度
	private String ifHurryUp;				//是否极速达
	private String hurryUpArea;				//极速达范围
	private String hurryUpFee;				//极速达费用
	private String deliveryStr;	
	private String payType;					//在线支付类型
	
	private String isExpressDelivery;		//在线支付类型
	private String jsdMinute;				//在线支付类型
	
	/**
	 * 店铺经营信息保存
	 * @date 2017-8-15
	 */
	public void saveTwo(){
		Map<String, String> map = getPara();
		map.put("shopId", shopId);
		map.put("payOnline", payOnline);
		map.put("startMoney", startMoney);
		map.put("shopArea", shopArea);
		map.put("businessLicense", businessLicense);
		map.put("businessScope", businessScope);
		shopStatus = "3".equals(shopStatus) ? "3" : "2";
		map.put("shopStatus", shopStatus);
		map.put("pointX", pointX);
		map.put("pointY", pointY);
		map.put("ifHurryUp", ifHurryUp);
		map.put("status", status);
		map.put("isExpressDelivery", isExpressDelivery);
		map.put("jsdMinute", jsdMinute);
		if("0".equals(ifHurryUp)){
			hurryUpArea = null;
			hurryUpFee = null;
		}
		map.put("hurryUpArea", hurryUpArea);
		map.put("hurryUpFee", hurryUpFee);
		String corpId = map.get("corpId");
		List<Map<String, String>> deliveryList = new ArrayList<Map<String,String>>();
		if(!"".equals(deliveryStr)){
			String [] deliveryArr = deliveryStr.split(",");
			request = getRequest();
			for(int i=0;i<deliveryArr.length;i++){
				String area = request.getParameter("deliveryArea"+deliveryArr[i]);
				String fee = request.getParameter("deliveryFee"+deliveryArr[i]);
				String hour = request.getParameter("deliveryHour"+deliveryArr[i]);
				Map<String, String> m = new HashMap<String, String>();
				m.put("uuid", new UUID().toString());
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("area", area);
				m.put("fee", fee);
				m.put("hour", hour);
				m.put("num", (i+1)+"");
				deliveryList.add(m);
			}
		}
		List<Map<String, String>> payList = new ArrayList<Map<String,String>>();
		if(!"".equals(payType)){
			String [] payTypeArr = payType.split(",");
			request = getRequest();
			for(int i=0;i<payTypeArr.length;i++){
				Map<String, String> m = new HashMap<String, String>();
				m.put("uuid", new UUID().toString());
				m.put("corpId", corpId);
				m.put("shopId", shopId);
				m.put("payType", payTypeArr[i]);
				payList.add(m);
			}
		}
		String result = storeApplyService.saveTwo(map,deliveryList,payList);
		JSONObject json = new JSONObject();
		json.put("shopId", shopId);
		json.put("result", result);
		str2Resp(json.toString());
	}
	
	/**
	 * 店铺申请开户信息页
	 * @date 2017-8-15
	 */
	public String toStoreApplyThrPage(){
		request = this.getRequest();
		Map<String, String> map = new HashMap<String, String>();
		map.put("shopId", shopId);
		
		Map<String, Object> shopInfo  = storeApplyService.queryShopInfo(map);
		
		List<Map<String, Object>> bankList = storeApplyService.queryBankList();
		
		map.put("level", "2");
		List<Map<String, Object>> provinceList = storeApplyService.queryProvinceCityList(map);
		map.put("areaPcode",(String) shopInfo.get("SHOP_PROVINCE"));
		map.put("level", "3");
		List<Map<String, Object>> cityList = storeApplyService.queryProvinceCityList(map);
		map.put("areaPcode",(String) shopInfo.get("SHOP_CITY"));
		map.put("level", "4");
		List<Map<String, Object>> countyList = storeApplyService.queryProvinceCityList(map);
		request.setAttribute("provinceList", provinceList);
		request.setAttribute("cityList", cityList);
		request.setAttribute("countyList", countyList);
		
		request.setAttribute("bankList", bankList);
		request.setAttribute("shopInfo", shopInfo);
		return "toStoreApplyThrPage";
	}
	
	private String payOnline;
	private String bankCode;
	private String provinceCode;
	private String cityCode;
	private String bankAccountName;
	private String bankAccount;
	private String startMoney;

	/**
	 * 开户信息保存
	 * @date 2017-8-15
	 */
	public void saveThr(){
		String userCode=getUserCodeFromSession();
		Map<String, String> map = new HashMap<String, String>();
		map.put("userCode", userCode);
		map.put("shopId", shopId);
		map.put("bankCode", bankCode);
		map.put("provinceCode", provinceCode);
		map.put("cityCode", cityCode);
		map.put("bankAccountName", bankAccountName);
		map.put("bankAccount", bankAccount);
		map.put("shopStatus", "3");
		map.put("applyStatus", "1");
		status = status == null || "".equals(status) ? "0" : status;
		map.put("registerStatus", status);
		String result = storeApplyService.saveThr(map);
		JSONObject json = new JSONObject();
		json.put("shopId", shopId);
		json.put("result", result);
		str2Resp(json.toString());
	}
	
	/**
	 * 店铺详情页
	 * @date 2017-8-15
	 */
	public String toStoreInfo(){
		
		request = this.getRequest();
		
		Map<String, Object> shopInfo = new HashMap<String, Object>();
		Map<String, String> map = new HashMap<String, String>();
		
		if(shopId == null || "".equals(shopId)){
			String userCode = getUserCodeFromSession();
			UserInfoBean userInfo = loginService.getUserInfo(userCode);
			shopId = userInfo.getShopId();
		}
		
		map.put("shopId", shopId);
		shopInfo = storeApplyService.queryShopInfo(map);
		map.put("level", "2");
		List<Map<String, Object>> provinceList = storeApplyService.queryProvinceCityList(map);
		map.put("areaPcode",(String) shopInfo.get("SHOP_PROVINCE"));
		map.put("level", "3");
		List<Map<String, Object>> cityList = storeApplyService.queryProvinceCityList(map);
		map.put("areaPcode",(String) shopInfo.get("SHOP_CITY"));
		map.put("level", "4");
		List<Map<String, Object>> countyList = storeApplyService.queryProvinceCityList(map);
		
		String areaCode = "";
		String areaName = "";
		List<Map<String, Object>> areaCodes = storeApplyService.queryCheckList(map);
		if(areaCodes.size() > 0){
			for (Map<String, Object> aMap : areaCodes) {
				areaCode += aMap.get("AREA_CODE").toString() + ",";
				areaName += aMap.get("ANAME").toString() + ",";
			}
			if(!"".equals(areaCode)){
				areaCode = areaCode.substring(0,areaCode.length() - 1);
				areaName = areaName.substring(0,areaName.length() - 1);
			}
		}
		
		shopInfo = storeApplyService.queryShopInfo(map);
		List<Map<String, Object>> deliveryFeeList = storeApplyService.queryDeliveryFee(map);
		List<Map<String, Object>> bankList = storeApplyService.queryBankList();
		map.put("areaPcode",(String) shopInfo.get("BANK_PROVINCE"));
		map.put("level", "3");
		List<Map<String, Object>> bankCityList = storeApplyService.queryProvinceCityList(map);
		
		Map<String, Object> obj = storeApplyService.getBankInfo(map);
		int status = obj == null ? -1 : (Integer) obj.get("STATUS");
		String statusName = "";
		if(status==0) statusName = "未审核";
		else if(status==1) statusName = "已审核";
		else if(status==2) statusName = "审核未通过";
		
		request.setAttribute("bankList", bankList);
		
		request.setAttribute("areaCode", areaCode);
		request.setAttribute("areaName", areaName);
		
		request.setAttribute("provinceList", provinceList);
		request.setAttribute("cityList", cityList);
		request.setAttribute("bankCityList", bankCityList);
		request.setAttribute("countyList", countyList);
		request.setAttribute("shopInfo", shopInfo);
		request.setAttribute("deliveryFeeList", deliveryFeeList);
		request.setAttribute("statusName", statusName);
		
		return "toStoreInfo";
	}
	
	private String shopStatus;
	private String showFlag;
	
	/**
	 * 编辑跳转控制页
	 * @date 2017-8-15
	 */
	public String getStorePage(){
		request = getRequest();
		Map<String, Object> shopInfo = new HashMap<String, Object>();
		Map<String, String> map = new HashMap<String, String>();
		if(shopId == null || "".equals(shopId)){
			String userCode = getUserCodeFromSession();
			UserInfoBean userInfo = loginService.getUserInfo(userCode);
			shopId = userInfo.getShopId();
		}
		map.put("level", "2");
		List<Map<String, Object>> provinceList = storeApplyService.queryProvinceCityList(map);
		if(!"".equals(shopId)){
			map.put("shopId", shopId);
			shopInfo = storeApplyService.queryShopInfo(map);
			map.put("areaPcode",(String) shopInfo.get("SHOP_PROVINCE"));
			map.put("level", "3");
			List<Map<String, Object>> cityList = storeApplyService.queryProvinceCityList(map);
			map.put("areaPcode",(String) shopInfo.get("SHOP_CITY"));
			map.put("level", "4");
			List<Map<String, Object>> countyList = storeApplyService.queryProvinceCityList(map);
			request.setAttribute("cityList", cityList);
			request.setAttribute("countyList", countyList);
			request.setAttribute("shopInfo", shopInfo);
			String areaName = "";
			String areaCode = "";
			List<Map<String, Object>> areaCodes = storeApplyService.queryCheckList(map);
			if(areaCodes.size() > 0){
				for (Map<String, Object> aMap : areaCodes) {
					areaCode += aMap.get("AREA_CODE").toString() + ",";
					areaName += aMap.get("ANAME").toString() + ",";
				}
				if(!"".equals(areaCode)){
					areaCode = areaCode.substring(0,areaCode.length() - 1);
					areaName = areaName.substring(0,areaName.length() - 1);
				}
			}
			request.setAttribute("areaCode", areaCode);
			request.setAttribute("areaName", areaName);
			map.put("areaPcode",(String) shopInfo.get("BANK_PROVINCE"));
			shopStatus = shopStatus == null ||shopStatus.equals("") ? shopInfo.get("SHOP_STATUS").toString() : shopStatus;
		}
		request.setAttribute("provinceList", provinceList);
		request.setAttribute("showFlag", showFlag);
		
		List<Map<String, Object>> bankList = storeApplyService.queryBankList();
		request.setAttribute("bankList", bankList);
		map.put("level", "3");
		List<Map<String, Object>> bankCityList = storeApplyService.queryProvinceCityList(map);
		request.setAttribute("bankCityList", bankCityList);
		
		List<Map<String, Object>> deliveryFeeList = storeApplyService.queryDeliveryFee(map);
		request.setAttribute("deliveryFeeList", deliveryFeeList);
		
		shopStatus = shopStatus == null ||shopStatus.equals("") ? "0" : shopStatus;
		
		String result = "";
		if("3".equals(shopStatus)){
			result = "toStoreInfo";
			Map<String, Object> obj = storeApplyService.getBankInfo(map);
			int status = obj == null ? -1 : (Integer) obj.get("STATUS");
			String statusName = "";
			if(status==0) statusName = "未审核";
			else if(status==1) statusName = "已审核";
			else if(status==2) statusName = "审核未通过";
			request.setAttribute("statusName", statusName);
		}else if("2".equals(shopStatus)){
			result = "toStoreApplyThrPage";
		}else if("1".equals(shopStatus)) {
			result = "toStoreApplyTwoPage";
		}else if("0".equals(shopStatus)) {
			result = "toStoreApplyOnePage";
		}
		return result;
	}
	
	/**
	 * 开户信息修改保存
	 * @date 2017-8-15
	 */
	public void saveBank(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("shopId", shopId);
		map.put("corpId", getBaseCorpId());
		map.put("payOnline", payOnline);
		map.put("startMoney", startMoney);
		map.put("bankCode", bankCode);
		map.put("provinceCode", provinceCode);
		map.put("cityCode", cityCode);
		map.put("bankAccountName", bankAccountName);
		map.put("bankAccount", bankAccount);
		map.put("shopStatus", "3");
		map.put("applyStatus", "1");
		String result = storeApplyService.saveBank(map);
		JSONObject json = new JSONObject();
		json.put("shopId", shopId);
		json.put("result", result);
		str2Resp(json.toString());
	}
	
	public Map<String, String> getPara(){
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId = userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("userCode", userCode);
		map.put("shopId", shopId);
		map.put("corpId", corpId);
		return map;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getAreaPcode() {
		return areaPcode;
	}

	public void setAreaPcode(String areaPcode) {
		this.areaPcode = areaPcode;
	}

	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public String getShopName() {
		return shopName;
	}

	public void setShopName(String shopName) {
		this.shopName = shopName;
	}

	public String getLegalPerson() {
		return legalPerson;
	}

	public void setLegalPerson(String legalPerson) {
		this.legalPerson = legalPerson;
	}

	public String getShopProvince() {
		return shopProvince;
	}

	public void setShopProvince(String shopProvince) {
		this.shopProvince = shopProvince;
	}

	public String getShopCity() {
		return shopCity;
	}

	public void setShopCity(String shopCity) {
		this.shopCity = shopCity;
	}

	public String getShopCounty() {
		return shopCounty;
	}

	public void setShopCounty(String shopCounty) {
		this.shopCounty = shopCounty;
	}

	public String getShopAddr() {
		return shopAddr;
	}

	public void setShopAddr(String shopAddr) {
		this.shopAddr = shopAddr;
	}

	public String getShopTel() {
		return shopTel;
	}

	public void setShopTel(String shopTel) {
		this.shopTel = shopTel;
	}

	public String getShopDesc() {
		return shopDesc;
	}

	public void setShopDesc(String shopDesc) {
		this.shopDesc = shopDesc;
	}

	public String getIdcardF() {
		return idcardF;
	}

	public void setIdcardF(String idcardF) {
		this.idcardF = idcardF;
	}

	public String getIdcardB() {
		return idcardB;
	}

	public void setIdcardB(String idcardB) {
		this.idcardB = idcardB;
	}

	public String getShopArea() {
		return shopArea;
	}

	public void setShopArea(String shopArea) {
		this.shopArea = shopArea;
	}

	public String getShopLogo() {
		return shopLogo;
	}

	public void setShopLogo(String shopLogo) {
		this.shopLogo = shopLogo;
	}

	public String getShopPic() {
		return shopPic;
	}

	public void setShopPic(String shopPic) {
		this.shopPic = shopPic;
	}

	public String getBusinessLicense() {
		return businessLicense;
	}

	public void setBusinessLicense(String businessLicense) {
		this.businessLicense = businessLicense;
	}

	public String getBusinessScope() {
		return businessScope;
	}

	public void setBusinessScope(String businessScope) {
		this.businessScope = businessScope;
	}

	public String getPayOnline() {
		return payOnline;
	}

	public void setPayOnline(String payOnline) {
		this.payOnline = payOnline;
	}

	public String getBankCode() {
		return bankCode;
	}

	public void setBankCode(String bankCode) {
		this.bankCode = bankCode;
	}

	public String getProvinceCode() {
		return provinceCode;
	}

	public void setProvinceCode(String provinceCode) {
		this.provinceCode = provinceCode;
	}

	public String getCityCode() {
		return cityCode;
	}

	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}

	public String getBankAccountName() {
		return bankAccountName;
	}

	public void setBankAccountName(String bankAccountName) {
		this.bankAccountName = bankAccountName;
	}

	public String getBankAccount() {
		return bankAccount;
	}

	public void setBankAccount(String bankAccount) {
		this.bankAccount = bankAccount;
	}

	public String getStartMoney() {
		return startMoney;
	}

	public void setStartMoney(String startMoney) {
		this.startMoney = startMoney;
	}

	public String getShopStatus() {
		return shopStatus;
	}

	public void setShopStatus(String shopStatus) {
		this.shopStatus = shopStatus;
	}

	public String getShowFlag() {
		return showFlag;
	}

	public void setShowFlag(String showFlag) {
		this.showFlag = showFlag;
	}

	public String getPointX() {
		return pointX;
	}

	public void setPointX(String pointX) {
		this.pointX = pointX;
	}

	public String getPointY() {
		return pointY;
	}

	public void setPointY(String pointY) {
		this.pointY = pointY;
	}

	public String getIfHurryUp() {
		return ifHurryUp;
	}

	public void setIfHurryUp(String ifHurryUp) {
		this.ifHurryUp = ifHurryUp;
	}

	public String getHurryUpArea() {
		return hurryUpArea;
	}

	public void setHurryUpArea(String hurryUpArea) {
		this.hurryUpArea = hurryUpArea;
	}

	public String getHurryUpFee() {
		return hurryUpFee;
	}

	public void setHurryUpFee(String hurryUpFee) {
		this.hurryUpFee = hurryUpFee;
	}

	public String getDeliveryStr() {
		return deliveryStr;
	}

	public void setDeliveryStr(String deliveryStr) {
		this.deliveryStr = deliveryStr;
	}

	public String getPayType() {
		return payType;
	}

	public void setPayType(String payType) {
		this.payType = payType;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getIsExpressDelivery() {
		return isExpressDelivery;
	}

	public void setIsExpressDelivery(String isExpressDelivery) {
		this.isExpressDelivery = isExpressDelivery;
	}

	public String getJsdMinute() {
		return jsdMinute;
	}

	public void setJsdMinute(String jsdMinute) {
		this.jsdMinute = jsdMinute;
	}

}
